package cn.ibizlab.odoo.core.odoo_stock.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Istock_picking_type;
import cn.ibizlab.odoo.core.odoo_stock.domain.Stock_picking_type;
import cn.ibizlab.odoo.core.odoo_stock.filter.Stock_picking_typeSearchContext;
import cn.ibizlab.odoo.core.odoo_stock.service.IStock_picking_typeService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[拣货类型] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Stock_picking_typeServiceImpl implements IStock_picking_typeService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Istock_picking_typeClientService stock_picking_typeClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Istock_picking_type clientModel = stock_picking_typeClientService.createModel();
        clientModel.setId(id);
        stock_picking_typeClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Stock_picking_type stock_picking_type) {
        boolean bOk = false ;
        Istock_picking_type clientModel = this.convert2Model(stock_picking_type,null);
        stock_picking_typeClientService.update(clientModel) ;
        this.convert2Domain(clientModel,stock_picking_type) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Stock_picking_type> stock_picking_types, int batchSize) {

                        //stock_picking_typeClientService.createBatch(stock_picking_types,batchSize) ;

    }

    @Override
    public void updateBatch(List<Stock_picking_type> stock_picking_types, int batchSize) {

        //stock_picking_typeClientService.updateBatch(stock_picking_types,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //stock_picking_typeClientService.removeBatch(stock_picking_types) ;

    }

    @Override
    public boolean create(Stock_picking_type stock_picking_type) {
        boolean bOk = false ;
        Istock_picking_type clientModel = this.convert2Model(stock_picking_type,null);
        stock_picking_typeClientService.create(clientModel) ;
        this.convert2Domain(clientModel,stock_picking_type);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Stock_picking_type get(Integer id) {
        Istock_picking_type clientModel = stock_picking_typeClientService.createModel();
        clientModel.setId(id);
        stock_picking_typeClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Stock_picking_type get(Stock_picking_type stock_picking_type) {
        Stock_picking_type retstock_picking_type = this.get(stock_picking_type.getId()) ;
    	BeanCopier copier=BeanCopier.create(retstock_picking_type.getClass(), stock_picking_type.getClass(), false);
        copier.copy(retstock_picking_type, stock_picking_type, null);
        return stock_picking_type ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Stock_picking_type> searchDefault(Stock_picking_typeSearchContext context) {
        List<Stock_picking_type> list = new ArrayList<Stock_picking_type>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_picking_type> clientModelList = stock_picking_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_picking_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_picking_type>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_picking_type> selectByOdoo_create(Integer id){
        Stock_picking_typeSearchContext context = new Stock_picking_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_picking_type> list = new ArrayList<Stock_picking_type>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_picking_type> clientModelList = stock_picking_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_picking_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_picking_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Stock_picking_type> selectByOdoo_write(Integer id){
        Stock_picking_typeSearchContext context = new Stock_picking_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_picking_type> list = new ArrayList<Stock_picking_type>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_picking_type> clientModelList = stock_picking_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_picking_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_picking_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Stock_picking_type> selectByOdoo_default_location_dest(Integer id){
        Stock_picking_typeSearchContext context = new Stock_picking_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_picking_type> list = new ArrayList<Stock_picking_type>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_picking_type> clientModelList = stock_picking_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_picking_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_picking_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Stock_picking_type> selectByOdoo_default_location_src(Integer id){
        Stock_picking_typeSearchContext context = new Stock_picking_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_picking_type> list = new ArrayList<Stock_picking_type>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_picking_type> clientModelList = stock_picking_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_picking_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_picking_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据拣货类型查询
     */
    @Override
    public Page<Stock_picking_type> selectByOdoo_return_picking_type(Integer id){
        Stock_picking_typeSearchContext context = new Stock_picking_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_picking_type> list = new ArrayList<Stock_picking_type>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_picking_type> clientModelList = stock_picking_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_picking_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_picking_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据仓库查询
     */
    @Override
    public Page<Stock_picking_type> selectByOdoo_warehouse(Integer id){
        Stock_picking_typeSearchContext context = new Stock_picking_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Stock_picking_type> list = new ArrayList<Stock_picking_type>();
        Page<cn.ibizlab.odoo.core.client.model.Istock_picking_type> clientModelList = stock_picking_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Istock_picking_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Stock_picking_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Istock_picking_type convert2Model(Stock_picking_type domain , Istock_picking_type model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = stock_picking_typeClientService.createModel() ;
        }

         if(domain.getShow_operationsDirtyFlag())
            model.setShow_operations(domain.getShow_operations());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getSequence_idDirtyFlag())
            model.setSequence_id(domain.getSequence_id());
         if(domain.getUse_create_lotsDirtyFlag())
            model.setUse_create_lots(domain.getUse_create_lots());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getBarcodeDirtyFlag())
            model.setBarcode(domain.getBarcode());
         if(domain.getShow_reservedDirtyFlag())
            model.setShow_reserved(domain.getShow_reserved());
         if(domain.getRate_picking_backordersDirtyFlag())
            model.setRate_picking_backorders(domain.getRate_picking_backorders());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getCount_picking_backordersDirtyFlag())
            model.setCount_picking_backorders(domain.getCount_picking_backorders());
         if(domain.getCount_mo_todoDirtyFlag())
            model.setCount_mo_todo(domain.getCount_mo_todo());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getShow_entire_packsDirtyFlag())
            model.setShow_entire_packs(domain.getShow_entire_packs());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getCount_mo_waitingDirtyFlag())
            model.setCount_mo_waiting(domain.getCount_mo_waiting());
         if(domain.getCount_picking_draftDirtyFlag())
            model.setCount_picking_draft(domain.getCount_picking_draft());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getCount_picking_lateDirtyFlag())
            model.setCount_picking_late(domain.getCount_picking_late());
         if(domain.getCount_pickingDirtyFlag())
            model.setCount_picking(domain.getCount_picking());
         if(domain.getCount_picking_readyDirtyFlag())
            model.setCount_picking_ready(domain.getCount_picking_ready());
         if(domain.getLast_done_pickingDirtyFlag())
            model.setLast_done_picking(domain.getLast_done_picking());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCount_picking_waitingDirtyFlag())
            model.setCount_picking_waiting(domain.getCount_picking_waiting());
         if(domain.getCodeDirtyFlag())
            model.setCode(domain.getCode());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getRate_picking_lateDirtyFlag())
            model.setRate_picking_late(domain.getRate_picking_late());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getCount_mo_lateDirtyFlag())
            model.setCount_mo_late(domain.getCount_mo_late());
         if(domain.getUse_existing_lotsDirtyFlag())
            model.setUse_existing_lots(domain.getUse_existing_lots());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getDefault_location_src_id_textDirtyFlag())
            model.setDefault_location_src_id_text(domain.getDefault_location_src_id_text());
         if(domain.getReturn_picking_type_id_textDirtyFlag())
            model.setReturn_picking_type_id_text(domain.getReturn_picking_type_id_text());
         if(domain.getWarehouse_id_textDirtyFlag())
            model.setWarehouse_id_text(domain.getWarehouse_id_text());
         if(domain.getDefault_location_dest_id_textDirtyFlag())
            model.setDefault_location_dest_id_text(domain.getDefault_location_dest_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getDefault_location_dest_idDirtyFlag())
            model.setDefault_location_dest_id(domain.getDefault_location_dest_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWarehouse_idDirtyFlag())
            model.setWarehouse_id(domain.getWarehouse_id());
         if(domain.getDefault_location_src_idDirtyFlag())
            model.setDefault_location_src_id(domain.getDefault_location_src_id());
         if(domain.getReturn_picking_type_idDirtyFlag())
            model.setReturn_picking_type_id(domain.getReturn_picking_type_id());
        return model ;
    }

    public Stock_picking_type convert2Domain( Istock_picking_type model ,Stock_picking_type domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Stock_picking_type();
        }

        if(model.getShow_operationsDirtyFlag())
            domain.setShow_operations(model.getShow_operations());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getSequence_idDirtyFlag())
            domain.setSequence_id(model.getSequence_id());
        if(model.getUse_create_lotsDirtyFlag())
            domain.setUse_create_lots(model.getUse_create_lots());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getBarcodeDirtyFlag())
            domain.setBarcode(model.getBarcode());
        if(model.getShow_reservedDirtyFlag())
            domain.setShow_reserved(model.getShow_reserved());
        if(model.getRate_picking_backordersDirtyFlag())
            domain.setRate_picking_backorders(model.getRate_picking_backorders());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getCount_picking_backordersDirtyFlag())
            domain.setCount_picking_backorders(model.getCount_picking_backorders());
        if(model.getCount_mo_todoDirtyFlag())
            domain.setCount_mo_todo(model.getCount_mo_todo());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getShow_entire_packsDirtyFlag())
            domain.setShow_entire_packs(model.getShow_entire_packs());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getCount_mo_waitingDirtyFlag())
            domain.setCount_mo_waiting(model.getCount_mo_waiting());
        if(model.getCount_picking_draftDirtyFlag())
            domain.setCount_picking_draft(model.getCount_picking_draft());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getCount_picking_lateDirtyFlag())
            domain.setCount_picking_late(model.getCount_picking_late());
        if(model.getCount_pickingDirtyFlag())
            domain.setCount_picking(model.getCount_picking());
        if(model.getCount_picking_readyDirtyFlag())
            domain.setCount_picking_ready(model.getCount_picking_ready());
        if(model.getLast_done_pickingDirtyFlag())
            domain.setLast_done_picking(model.getLast_done_picking());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCount_picking_waitingDirtyFlag())
            domain.setCount_picking_waiting(model.getCount_picking_waiting());
        if(model.getCodeDirtyFlag())
            domain.setCode(model.getCode());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getRate_picking_lateDirtyFlag())
            domain.setRate_picking_late(model.getRate_picking_late());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getCount_mo_lateDirtyFlag())
            domain.setCount_mo_late(model.getCount_mo_late());
        if(model.getUse_existing_lotsDirtyFlag())
            domain.setUse_existing_lots(model.getUse_existing_lots());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getDefault_location_src_id_textDirtyFlag())
            domain.setDefault_location_src_id_text(model.getDefault_location_src_id_text());
        if(model.getReturn_picking_type_id_textDirtyFlag())
            domain.setReturn_picking_type_id_text(model.getReturn_picking_type_id_text());
        if(model.getWarehouse_id_textDirtyFlag())
            domain.setWarehouse_id_text(model.getWarehouse_id_text());
        if(model.getDefault_location_dest_id_textDirtyFlag())
            domain.setDefault_location_dest_id_text(model.getDefault_location_dest_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getDefault_location_dest_idDirtyFlag())
            domain.setDefault_location_dest_id(model.getDefault_location_dest_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWarehouse_idDirtyFlag())
            domain.setWarehouse_id(model.getWarehouse_id());
        if(model.getDefault_location_src_idDirtyFlag())
            domain.setDefault_location_src_id(model.getDefault_location_src_id());
        if(model.getReturn_picking_type_idDirtyFlag())
            domain.setReturn_picking_type_id(model.getReturn_picking_type_id());
        return domain ;
    }


}
